"""
Copyright (c) 2022 Beijing Jiaotong University
PhotLab is licensed under [Open Source License].
You can use this software according to the terms and conditions of the [Open Source License].
You may obtain a copy of [Open Source License] at: [https://open.source.license/]

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.

See the [Open Source License] for more details.

Author: Chunyu Li
Created: 2022/9/6
Supported by: National Key Research and Development Program of China
"""

import numpy as np
from scipy.signal import resample_poly
from ...optical import adc_resolution


def adc_noise(signals, sampling_rate, adc_sample_rate, adc_resolution_bits):
    """加入ADC的量化噪声"""

    return_signals = []

    for signal in signals:
        # 重采样改变采样率为ADC采样率，模拟进入ADC
        signal = resample_poly(signal, int(adc_sample_rate), int(sampling_rate))

        # 对信号量化，添加ADC造成的量化噪声
        signal = adc_resolution(signal, adc_resolution_bits)

        # 减去信号量化后的直流
        signal = signal - np.mean(signal)

        # 将信号采样率重采样为原来的采样率
        signal = resample_poly(signal, int(sampling_rate), int(adc_sample_rate))

        return_signals.append(signal)

    return return_signals